package cn.edu.neusoft.wuguangsheng.flowerimpression.adapter;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.squareup.picasso.Picasso;

import java.util.List;

import cn.edu.neusoft.wuguangsheng.flowerimpression.bean.CartGoodsBean;
import cn.edu.neusoft.wuguangsheng.flowerimpression.bean.SuccessBean;
import cn.edu.neusoft.wuguangsheng.flowerimpression.listener.TListener;
import cn.edu.neusoft.wuguangsheng.flowerimpression.model.CartAddGoodsNumModel;
import cn.edu.neusoft.wuguangsheng.flowerimpression.model.CartDescGoodsNumModel;
import cn.edu.neusoft.wuguangsheng.flowerimpression.model.DeleteCartGoodsModel;
import cn.edu.neusoft.wuguangsheng.flowerimpression.model.SetCartGoodsFlagModel;
import cn.edu.neusoft.wuguangsheng.flowerimpression.service.Service;

public class CartGoodsAdapter extends BaseAdapter<CartGoodsBean> {

    private Handler handler;

    public CartGoodsAdapter(Context context, List<CartGoodsBean> items, int layoutResourse,Handler handler) {
        super(context, items, layoutResourse);
        this.handler = handler;
    }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView= LayoutInflater.from(context).inflate(layoutResourse,parent,false);
        CartGoodsViewHolder cartGoodsViewHolder = new CartGoodsViewHolder(itemView);
        return cartGoodsViewHolder;
    }
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        final CartGoodsViewHolder cartGoodsViewHolder=(CartGoodsViewHolder) holder;
        final CartGoodsBean cartGoodsBean=items.get(position);
        final String goodsname = cartGoodsBean.getGood_name();
        final double goodsprice = cartGoodsBean.getGood_price();
        final int goodsnum = cartGoodsBean.getCart_goodnum();
        final String img = Service.BASE_URL+"flower_impression/public/uploads/"+cartGoodsBean.getGood_photo();
        final String buyflag = cartGoodsBean.getCart_buyflag();
        final String carid = cartGoodsBean.getCart_id();




        if (buyflag.equals("1")){
            cartGoodsViewHolder.cartitem_flag.setChecked(true);
        }
        else{
            cartGoodsViewHolder.cartitem_flag.setChecked(false);
        }
        cartGoodsViewHolder.cartitem_name.setText(goodsname);
        cartGoodsViewHolder.cartitem_price.setText("￥"+goodsprice);
        cartGoodsViewHolder.cartitem_num.setText(String.valueOf(goodsnum));
        Picasso.with(context).load(img).into(cartGoodsViewHolder.cartitem_img);

        cartGoodsViewHolder.carttiem_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DeleteCartGoodsModel deleteCartGoodsModel = new DeleteCartGoodsModel();
                deleteCartGoodsModel.deleteCartGoods(carid, new TListener<SuccessBean>() {
                    @Override
                    public void onResponse(SuccessBean successBean) {
                        if (successBean.getSuccess().equals("1")){
                            items.remove(position);
                            notifyItemRemoved(position);
                            notifyItemRangeChanged(0,items.size());
                            Message message = Message.obtain();
                            message.what = 0;
                            handler.sendMessage(message);
                        }
                        else{
                            Toast.makeText(context, "失败", Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onFail(String msg) {
                        Toast.makeText(context, "失败1", Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });

        cartGoodsViewHolder.cartitem_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                final int num = cartGoodsBean.getCart_goodnum()+1;
                CartAddGoodsNumModel cartAddGoodsNumModel = new CartAddGoodsNumModel();
                cartAddGoodsNumModel.cartAddGoodsNum(carid, new TListener<SuccessBean>() {
                    @Override
                    public void onResponse(SuccessBean successBean) {
                        if (successBean.getSuccess().equals("1")){
                            cartGoodsBean.setCart_goodnum(num);
                            cartGoodsViewHolder.cartitem_num.setText(String.valueOf(num));
                            Message message = Message.obtain();
                            message.what = 0;
                            handler.sendMessage(message);
                        }else{
                            Toast.makeText(context, "加载失败", Toast.LENGTH_SHORT).show();
                        }

                    }

                    @Override
                    public void onFail(String msg) {
                        Toast.makeText(context, "加载失败1", Toast.LENGTH_SHORT).show();
                    }
                });

            }
        });

        cartGoodsViewHolder.cartitem_desc.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (cartGoodsBean.getCart_goodnum() == 1){
                    Toast.makeText(context,"最少选择一份",Toast.LENGTH_SHORT).show();

//                    final  int i=1;
//                    CartDescGoodsNumModel cartDescGoodsNumModel = new CartDescGoodsNumModel();
//                    cartDescGoodsNumModel.cartDescGoodsNum(carid, new TListener<SuccessBean>() {
//                        @Override
//                        public void onResponse(SuccessBean successBean) {
//
//
//                        }
//                        @Override
//                        public void onFail(String msg) {
//
//                        }
//                    });
                }
                else if (cartGoodsBean.getCart_goodnum() > 1){
                    final int num = cartGoodsBean.getCart_goodnum()-1;
                    CartDescGoodsNumModel cartDescGoodsNumModel = new CartDescGoodsNumModel();
                    cartDescGoodsNumModel.cartDescGoodsNum(carid, new TListener<SuccessBean>() {
                        @Override
                        public void onResponse(SuccessBean successBean) {
                            if (successBean.getSuccess().equals("1")){
                                cartGoodsBean.setCart_goodnum(num);
                                cartGoodsViewHolder.cartitem_num.setText(String.valueOf(num));
                                Message message = Message.obtain();
                                message.what = 0;
                                handler.sendMessage(message);
                            }else{
                                Toast.makeText(context, "加载失败", Toast.LENGTH_SHORT).show();
                            }

                        }

                        @Override
                        public void onFail(String msg) {
                            Toast.makeText(context, "加载失败1", Toast.LENGTH_SHORT).show();
                        }
                    });
                }



            }
        });

        cartGoodsViewHolder.cartitem_flag.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Boolean q = cartGoodsViewHolder.cartitem_flag.isChecked();
                String flag = "0";
                if (buyflag.equals("1")){
                    flag = buyflag;
                }
                else{
                    flag ="0";
                }
                if (q == true){
                    SetCartGoodsFlagModel setCartGoodsFlagModel = new SetCartGoodsFlagModel();
                    setCartGoodsFlagModel.setCartGoodsFlag(carid, "1", new TListener<SuccessBean>() {
                        @Override
                        public void onResponse(SuccessBean successBean) {
                            if (successBean.getSuccess().equals("1")) {

                                Message message = Message.obtain();
                                message.what = 0;
                                handler.sendMessage(message);
                            } else {
                                Toast.makeText(context, "加载失败", Toast.LENGTH_SHORT).show();
                            }
                        }

                        @Override
                        public void onFail(String msg) {
                            Toast.makeText(context, "111", Toast.LENGTH_SHORT).show();
                        }
                    });
                }
                else{
                    SetCartGoodsFlagModel setCartGoodsFlagModel = new SetCartGoodsFlagModel();
                    setCartGoodsFlagModel.setCartGoodsFlag(carid, "0", new TListener<SuccessBean>() {
                        @Override
                        public void onResponse(SuccessBean successBean) {
                            if (successBean.getSuccess().equals("1")) {

                                Message message = Message.obtain();
                                message.what = 0;
                                handler.sendMessage(message);
                            } else {
                                Toast.makeText(context, "加载失败", Toast.LENGTH_SHORT).show();
                            }
                        }

                        @Override
                        public void onFail(String msg) {
                            Toast.makeText(context, "222", Toast.LENGTH_SHORT).show();
                        }
                    });

                }
            }
        });
    }


}
